//
//  RedioViewController.m
//  Leisure
//
//  Created by 大展 on 16/4/18.
//  Copyright © 2016年 张展. All rights reserved.
//

#import "RedioViewController.h"

@interface RedioViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *scrArray;
@property (nonatomic, strong) CycleScrollView *cycleScrollView;
@end

@implementation RedioViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    [self fetchDataWithUrl:kRedioUrl];
    [self.tableView registerNib:[UINib nibWithNibName:@"RedioListCell" bundle:nil] forCellReuseIdentifier:@"redio_cell"];
}


//创建轮播图
- (void)creatCycleScrollView {
    NSLog(@"轮播图！！！");
    _cycleScrollView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight/3-64) animationDuration:5];

    NSMutableArray *viewsArray = [@[] mutableCopy];
    for (int i = 0; i < self.scrArray.count; ++i) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:_cycleScrollView.bounds];
        NSString *imgUrl = self.scrArray[i][@"img"];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imgUrl]];
        [viewsArray addObject:imageView];
    }
    [self.view addSubview:_cycleScrollView];

    _cycleScrollView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };

    _cycleScrollView.totalPagesCount = viewsArray.count;
}

- (void)fetchDataWithUrl:(NSString *)url {

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];


        [manager POST:url parameters:@{} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            NSArray *sourceArray = dic[@"data"][@"alllist"];

            for (NSDictionary *object in sourceArray) {

                RedioModel *model = [[RedioModel alloc]init];
                [model setValuesForKeysWithDictionary:object];

                [self.dataSource addObject:model];

            }
            [self.scrArray addObjectsFromArray:dic[@"data"][@"carousel"]];
            dispatch_async(dispatch_get_main_queue(), ^{

                [self.tableView reloadData];
                [self creatCycleScrollView];
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            });

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {


        }];
    });
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    RedioListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"redio_cell" forIndexPath:indexPath];

    [cell setCellWithModel:self.dataSource[indexPath.row]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    RedioDetailViewController *redioDetailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"RedioDetailViewController"];
    redioDetailVC.model = self.dataSource[indexPath.row];

    [[DrawViewController sharedManager].mainViewController.navigationController pushViewController:redioDetailVC animated:YES];
    
}

#pragma mark ------懒加载---------

- (NSMutableArray *)dataSource {

    if (!_dataSource) {

        _dataSource = [NSMutableArray array];
    }
    return  _dataSource;
}

- (NSMutableArray *)scrArray {

    if (!_scrArray) {

        _scrArray = [NSMutableArray array];
    }
    return _scrArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
